package compendiumAid.application.preferences
{
	import compendiumAid.domain.model.ApplicationModel;
	import compendiumAid.presentation.options.OptionsPM;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import org.alivepdf.layout.Orientation;
	import org.alivepdf.layout.Size;
	import org.alivepdf.layout.Unit;

	public class PreferencesCommand
	{
		[Inject]
		public var optionsPM:OptionsPM;
		
		[Inject]
		public var model:ApplicationModel;
		
		private var fileName:String = "CompendiumAid.preferences";
		
		[Command(selector="PreferencesEvent.LOAD")]
		public function loadPreferences(event:PreferencesEvent):void
		{
			var preferencesFile:File;
			var stream:FileStream;
			var preferencesObject:Object = {};
			
			try
			{
				preferencesFile = File.applicationStorageDirectory.resolvePath(fileName);
				stream = new FileStream();
				stream.open( preferencesFile, FileMode.READ );
				
				preferencesObject = stream.readObject();
			}
			catch (e:Error)
			{
				trace("Load Preferences Error - File: " + fileName + ". Error: " + e.message);
			}
			
			stream.close();
			
			loadDefaults(preferencesObject);
			
			distributePreferences(preferencesObject);
			
			if (optionsPM.autoLogin != true)
			{
				optionsPM.showLoginPopUp();
			}
		}
		
		[Command(selector="PreferencesEvent.SAVE")]
		public function savePreferences(event:PreferencesEvent):void
		{
			try
			{
				distributePreferences(event.preferencesObject);
				
				var preferencesFile:File = File.applicationStorageDirectory.resolvePath(fileName);
				var stream:FileStream = new FileStream(); 
				
				stream.open(preferencesFile, FileMode.WRITE);
				stream.truncate();
				stream.writeObject(event.preferencesObject);
			}
			catch (e:Error)
			{
				trace("Save Preferences Error - File: " + fileName + ". Error: " + e.message);
			}
			
			stream.close();
		}
		
		private function distributePreferences(preferencesObject:Object):void
		{
			var size:Size;
			
			switch (preferencesObject.paperType.label)
			{
				case Size.A3.label: 		size = Size.A3; 		break;
				case Size.A4.label: 		size = Size.A4; 		break;
				case Size.A5.label: 		size = Size.A5; 		break;
				case Size.LEGAL.label: 		size = Size.LEGAL; 		break;
				case Size.LETTER.label: 	size = Size.LETTER; 	break;
				case Size.TABLOID.label: 	size = Size.TABLOID; 	break;
				default: 					size = Size.LETTER; 	break;
			}
			
			optionsPM.username 			 = model.username 			= preferencesObject.username;
			optionsPM.password 			 = model.password 			= preferencesObject.password;
			optionsPM.rememberPass 		 = 							  preferencesObject.rememberPass;
			optionsPM.autoLogin 		 = 							  preferencesObject.autoLogin;
			optionsPM.elementsPerRow 	 = model.elementsPerRow 	= preferencesObject.elementsPerRow;
			optionsPM.elementOrientation = model.elementOrientation = preferencesObject.elementOrientation;
			optionsPM.paperType			 = model.paperType 			= size;
			optionsPM.marginSize		 = model.marginSize 		= preferencesObject.marginSize;
			optionsPM.unitType		 	 = model.unitType 			= preferencesObject.unitType;
		}
		
		private function loadDefaults(preferencesObject:Object):void
		{
			if (preferencesObject.elementOrientation == null)
			{
				preferencesObject.elementOrientation = Orientation.LANDSCAPE;
			}
			
			if (isNaN(preferencesObject.elementsPerRow))
			{
				preferencesObject.elementsPerRow = 3;
			}
			
			if (preferencesObject.paperType == null)
			{
				preferencesObject.paperType = Size.LETTER;
			}
			
			if (isNaN(preferencesObject.marginSize))
			{
				preferencesObject.marginSize = 0.2;
			}
			
			if (preferencesObject.unitType == null)
			{
				preferencesObject.unitType = Unit.INCHES;
			}
		}
	}
}